package gen.desktop.components;

import gen.core.Generator;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

public class GenByArraysPanel extends JPanel implements TemplateToolBarResponser{
	private static final String CATAGORY = "GenByArrays";
	private static final int MAX_ARRAYS_ROW_SIZE = 256;

	private static final long serialVersionUID = 1L;
	private JSplitPane splitPaneMain;
	private JSplitPane splitPaneSub;
	private JTextArea textAreaArrays;
	private JTextArea textAreaTemplate;
	private JTextArea textAreaResult;
	private JSeparator separator;
	private JPanel panel;

	/**
	 * Create the panel.
	 */
	public GenByArraysPanel() {
		setLayout(new BorderLayout(0, 0));

		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		add(toolBar, BorderLayout.NORTH);

		separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		toolBar.add(separator);

		JButton btnNewButton_gen = new JButton("生成");
		btnNewButton_gen.setIcon(new ImageIcon(GenByArraysPanel.class.getResource("/gen/desktop/icons/size16x16/cog.png")));
		btnNewButton_gen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GenByArraysPanel.this.generate();
			}
		});
		btnNewButton_gen.setFont(new Font("宋体", Font.PLAIN, 12));
		toolBar.add(btnNewButton_gen);

		splitPaneMain = new JSplitPane();
		splitPaneMain.setOrientation(JSplitPane.VERTICAL_SPLIT);
		add(splitPaneMain, BorderLayout.CENTER);

		splitPaneSub = new JSplitPane();
		splitPaneMain.setLeftComponent(splitPaneSub);

		JScrollPane scrollPane = new JScrollPane();
		splitPaneSub.setLeftComponent(scrollPane);

		textAreaArrays = new JTextArea();
		scrollPane.setViewportView(textAreaArrays);

		panel = new JPanel();
		splitPaneSub.setRightComponent(panel);
		panel.setLayout(new BorderLayout(0, 0));

		TemplateToolBar toolBar_1 = new TemplateToolBar(this, CATAGORY);
		panel.add(toolBar_1, BorderLayout.NORTH);

		JScrollPane scrollPane_1 = new JScrollPane();
		panel.add(scrollPane_1, BorderLayout.CENTER);

		textAreaTemplate = new JTextArea();
		scrollPane_1.setViewportView(textAreaTemplate);

		JScrollPane scrollPane_3 = new JScrollPane();
		splitPaneMain.setRightComponent(scrollPane_3);

		textAreaResult = new JTextArea();
		scrollPane_3.setViewportView(textAreaResult);
	}

	public void centerDivider() {
		splitPaneMain.setDividerLocation(0.5);
		splitPaneSub.setDividerLocation(0.5);
	}

	public void init() {
		centerDivider();
	}

	public void generate() {
		String arraysString = this.textAreaArrays.getText();
		String templateString = this.textAreaTemplate.getText();

		String[] rows = arraysString.split("\\n");
		String[][] arrays = new String[rows.length][MAX_ARRAYS_ROW_SIZE];

		final String DEFAULT_STRING = "";
		for (int i = 0; i < rows.length; i++) {
			for (int j = 0; j < MAX_ARRAYS_ROW_SIZE; j++) {
				arrays[i][j] = DEFAULT_STRING;
			}

			String[] tokens = rows[i].trim().split("\\s+");
			for (int j = 0; j < tokens.length; j++) {
				arrays[i][j] = tokens[j];
			}
		}

		try {
			Generator aGenerator = new Generator();
			aGenerator.init();
			String result = aGenerator.genByArrays(arrays, templateString);
			this.textAreaResult.setText(result);
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage(), "代码生成失败！", JOptionPane.ERROR_MESSAGE);
			return;
		}

	}

	@Override
	public void setTemplateText(String templateStr) {
		this.textAreaTemplate.setText(templateStr);
	}

	@Override
	public void setResultText(String resultStr) {
		this.textAreaResult.setText(resultStr);
	}

	@Override
	public String getTemplateText() {
		return this.textAreaTemplate.getText();
	}

	@Override
	public String getResultText() {
		return this.textAreaResult.getText();
	}

	@Override
	public void setSqlText(String sqlStr) {
	}

	@Override
	public String getSqlText() {
		return null;
	}
}
